package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.WeiboMapper;
import cn.tedu.egweibo1.pojo.dto.WeiboDetailVO;
import cn.tedu.egweibo1.pojo.dto.weiboDTO;
import cn.tedu.egweibo1.pojo.entity.User;
import cn.tedu.egweibo1.pojo.entity.Weibo;
import cn.tedu.egweibo1.pojo.vo.WeiboVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("v1/weibo/")
public class WeiboController {

    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("isnert")
    public int insertWeibo(@RequestBody weiboDTO weiboDTO, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        if(user == null){
            return 2;
        }
        Weibo weibo = new Weibo();
        weibo.setCreated(new Date());
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setUserId(user.getId());
        weiboMapper.insert(weibo);
        return 1;
    }

    @GetMapping("selectIndex")
    public List<WeiboVO> selectWeibo() {
        return weiboMapper.selectWeibo();
    }

    @GetMapping("selectById")
    public WeiboDetailVO selectWeiboById(Long id){
        return weiboMapper.selectWeiboById(id);
    }
}
